//
//  MainTabController.m
//  lvjin
//
//  Created by  on 12-5-17.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "MainTabController.h"
#import "MainTab.h"

@interface MainTabController()

@property (nonatomic, retain) MainTab * customTabBar;

- (void)hideRealTabBar;
- (void)initCustomTabBar;
- (void)selectTab:(UIButton *)button;
- (void)showCustomTabBar;
- (void)showFirst;

@end

@implementation MainTabController

@synthesize customTabBar;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initCustomTabBar];
    [self hideRealTabBar];
    [self showCustomTabBar];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)showCustomTabBar
{
    CGRect newTabBarFrame = CGRectMake(0, self.view.bounds.size.height - self.customTabBar.bounds.size.height, 320, self.customTabBar.frame.size.height);

    self.customTabBar.frame = newTabBarFrame;

    if (!self.customTabBar.superview)
    {
        [self.view addSubview:self.customTabBar];
    }
}

- (void)showFirst
{
    if (self.selectedIndex != 0)
    {
        self.selectedIndex = 0;
    }
    [self.customTabBar effectWithTag:0];
}

- (void)initCustomTabBar
{
    self.customTabBar = [MainTab getInstance];
    [self.customTabBar configClickWithTarget:self selector:@selector(selectTab:)];
    [self.customTabBar effectWithTag:0];
}

- (void)hideRealTabBar
{
    CGFloat temp = self.customTabBar.bounds.size.height - 43;
    for (UIView *view in self.view.subviews) {
        if ([view isKindOfClass:[UITabBar class]]) {
            view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y + temp , 320, 43 + temp);
            view.hidden = YES;
            break;
        }
        else
        {
            view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, 320, view.frame.size.height + temp);
        }
    }
}

- (void)selectTab:(UIButton *)button
{
    if (self.selectedIndex == button.tag)
    {
        return;
    }
    
    self.selectedIndex = button.tag;
    
    [self.customTabBar effectWithTag:button.tag];
}


- (void)showTabBar

{
    if (self.tabBarController.tabBar.hidden == NO)
    {
        return;
    }
    UIView *contentView;
    if ([[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]])
        contentView = [self.tabBarController.view.subviews objectAtIndex:1];
    else
        contentView = [self.tabBarController.view.subviews objectAtIndex:0];
    contentView.frame = CGRectMake(contentView.bounds.origin.x, contentView.bounds.origin.y,  contentView.bounds.size.width, contentView.bounds.size.height - self.tabBarController.tabBar.frame.size.height);
    self.tabBarController.tabBar.hidden = NO;
}

@end
